package com.kaisheng.web;


import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import com.kaisheng.entity.Project;
import com.kaisheng.entity.User;
import com.kaisheng.entity.UserProject;
import com.kaisheng.service.ProjectService;
import com.kaisheng.service.UserService;



public class SetServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		HttpSession session = request.getSession();
		String project_id = (String) session.getAttribute("project_id");
		
		ProjectService projectservice = new ProjectService();
		Project p = projectservice.findProject(project_id);
		request.setAttribute("p",p);
		
		
		
		ProjectService service = new ProjectService();
		UserProject userproject = service.findByProjectIdAndRole(project_id,"manager");
		
		ProjectService service2 = new ProjectService();
		List<UserProject> workers = service2.findAllWorkers(project_id,"manager");
		
	
		request.setAttribute("userproject",userproject);
		request.setAttribute("workers",workers);
		
		
		
		
		UserService userService = new UserService();
		List<User> userList = userService.findAll();
		
		request.setAttribute("userList", userList);
		request.getRequestDispatcher("/WEB-INF/views/edit_project.jsp").forward(request, response);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		
		HttpSession session = request.getSession();
		String project_id = (String) session.getAttribute("project_id");
		
		
		
		String name = request.getParameter("name");
		String desc = request.getParameter("desc");
		String manager = request.getParameter("manager");
		String workers = request.getParameter("workers");
		
		ProjectService service = new ProjectService();
		service.update(project_id,name,desc,manager,workers);
		
		response.sendRedirect("main.jspx");
		
		
		
		
	}


}
